<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>boost/python/init.hpp</title>
<link rel="stylesheet" href="../../boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../index.html" title="Boost.Python Reference Manual">
<link rel="up" href="../high_level_components.html" title="Chapter 2. High Level Components">
<link rel="prev" href="boost_python_exception_translato.html" title="boost/python/exception_translator.hpp">
<link rel="next" href="boost_python_iterator_hpp.html" title="boost/python/iterator.hpp">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr><td valign="top"><img alt="" width="" height="" src="../../images/boost.png"></td></tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="boost_python_exception_translato.html"><img src="../../images/prev.png" alt="Prev"></a><a accesskey="u" href="../high_level_components.html"><img src="../../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../images/home.png" alt="Home"></a><a accesskey="n" href="boost_python_iterator_hpp.html"><img src="../../images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="high_level_components.boost_python_init_hpp"></a><a class="link" href="boost_python_init_hpp.html" title="boost/python/init.hpp">boost/python/init.hpp</a>
</h2></div></div></div>
<div class="toc"><dl class="toc">
<dt><span class="section"><a href="boost_python_init_hpp.html#high_level_components.boost_python_init_hpp.introduction">Introduction</a></span></dt>
<dt><span class="section"><a href="boost_python_init_hpp.html#high_level_components.boost_python_init_hpp.class_template_init">Class
        template <code class="computeroutput"><span class="identifier">init</span></code></a></span></dt>
<dt><span class="section"><a href="boost_python_init_hpp.html#high_level_components.boost_python_init_hpp.class_template_optional">Class
        template <code class="computeroutput"><span class="identifier">optional</span></code> </a></span></dt>
<dt><span class="section"><a href="boost_python_init_hpp.html#high_level_components.boost_python_init_hpp.example">Example</a></span></dt>
</dl></div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="high_level_components.boost_python_init_hpp.introduction"></a><a class="link" href="boost_python_init_hpp.html#high_level_components.boost_python_init_hpp.introduction" title="Introduction">Introduction</a>
</h3></div></div></div>
<div class="toc"><dl class="toc"><dt><span class="section"><a href="boost_python_init_hpp.html#high_level_components.boost_python_init_hpp.introduction.init_expressions">init-expressions</a></span></dt></dl></div>
<p>
          &lt;boost/python/init.hpp&gt; defines the interface for exposing C++ constructors
          to Python as extension class <code class="computeroutput"><span class="identifier">__init__</span></code>
          functions.
        </p>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="high_level_components.boost_python_init_hpp.introduction.init_expressions"></a><a class="link" href="boost_python_init_hpp.html#high_level_components.boost_python_init_hpp.introduction.init_expressions" title="init-expressions">init-expressions</a>
</h4></div></div></div>
<p>
            An init-expression is used to describe a family of <code class="computeroutput"><span class="identifier">__init__</span></code>
            methods to be generated for an extension class, and the result has the
            following properties:
          </p>
<div class="variablelist">
<p class="title"><b></b></p>
<dl class="variablelist">
<dt><span class="term">docstring</span></dt>
<dd><p>
                  An <a class="link" href="../glossary.html#ntbs">ntbs</a> whose value will bound to the
                  method's <code class="computeroutput"><span class="identifier">__doc__</span></code>
                  attribute
                </p></dd>
<dt><span class="term">keywords</span></dt>
<dd><p>
                  A <a class="link" href="../function_invocation_and_creation.html#function_invocation_and_creation.boost_python_args_hpp.introduction.keyword_expressions" title="keyword-expressions">keyword-expression</a>
                  which will be used to name (a trailing subsequence of) the arguments
                  to the generated <code class="computeroutput"><span class="identifier">__init__</span></code>
                  function(s).
                </p></dd>
<dt><span class="term">call_policies</span></dt>
<dd><p>
                  An instance of a model of <a class="link" href="../concepts.html#concepts.callpolicies" title="CallPolicies">CallPolicies</a>.
                </p></dd>
<dt><span class="term">argument_types</span></dt>
<dd><p>
                  An MPL sequence of C++ argument types which will be used to construct
                  the wrapped C++ object. An init expression has one or more valid
                  prefixes which are given by a sequence of prefixes of its argument
                  types.
                </p></dd>
</dl>
</div>
</div>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="high_level_components.boost_python_init_hpp.class_template_init"></a><a class="link" href="boost_python_init_hpp.html#high_level_components.boost_python_init_hpp.class_template_init" title="Class template init">Class
        template <code class="computeroutput"><span class="identifier">init</span></code></a>
</h3></div></div></div>
<div class="toc"><dl class="toc">
<dt><span class="section"><a href="boost_python_init_hpp.html#high_level_components.boost_python_init_hpp.class_template_init.class_template_init_constructors">Class
          template <code class="computeroutput"><span class="identifier">init</span></code> constructors</a></span></dt>
<dt><span class="section"><a href="boost_python_init_hpp.html#high_level_components.boost_python_init_hpp.class_template_init.class_template_init_observer_fun">Class
          template <code class="computeroutput"><span class="identifier">init</span></code> observer
          functions</a></span></dt>
</dl></div>
<p>
          A MPL sequence which can be used to specify a family of one or more __init__
          functions. Only the last Ti supplied may be an instantiation of optional&lt;...&gt;.
        </p>
<pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">python</span>
<span class="special">{</span>
  <span class="keyword">template</span> <span class="special">&lt;</span><span class="identifier">T1</span> <span class="special">=</span> <span class="identifier">unspecified</span><span class="special">,...</span><span class="identifier">Tn</span> <span class="special">=</span> <span class="identifier">unspecified</span><span class="special">&gt;</span>
  <span class="keyword">struct</span> <span class="identifier">init</span>
  <span class="special">{</span>
    <span class="identifier">init</span><span class="special">(</span><span class="keyword">char</span> <span class="keyword">const</span><span class="special">*</span> <span class="identifier">doc</span> <span class="special">=</span> <span class="number">0</span><span class="special">);</span>
    <span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">Keywords</span><span class="special">&gt;</span> <span class="identifier">init</span><span class="special">(</span><span class="identifier">Keywords</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">kw</span><span class="special">,</span> <span class="keyword">char</span> <span class="keyword">const</span><span class="special">*</span> <span class="identifier">doc</span> <span class="special">=</span> <span class="number">0</span><span class="special">);</span>
    <span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">Keywords</span><span class="special">&gt;</span> <span class="identifier">init</span><span class="special">(</span><span class="keyword">char</span> <span class="keyword">const</span><span class="special">*</span> <span class="identifier">doc</span><span class="special">,</span> <span class="identifier">Keywords</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">kw</span><span class="special">);</span>

    <span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">CallPolicies</span><span class="special">&gt;</span>
    <span class="identifier">unspecified</span> <span class="keyword">operator</span><span class="special">[](</span><span class="identifier">CallPolicies</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">policies</span><span class="special">)</span> <span class="keyword">const</span>
  <span class="special">};</span>
<span class="special">}}</span>
</pre>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="high_level_components.boost_python_init_hpp.class_template_init.class_template_init_constructors"></a><a class="link" href="boost_python_init_hpp.html#high_level_components.boost_python_init_hpp.class_template_init.class_template_init_constructors" title="Class template init constructors">Class
          template <code class="computeroutput"><span class="identifier">init</span></code> constructors</a>
</h4></div></div></div>
<pre class="programlisting"><span class="identifier">init</span><span class="special">(</span><span class="keyword">char</span> <span class="keyword">const</span><span class="special">*</span> <span class="identifier">doc</span> <span class="special">=</span> <span class="number">0</span><span class="special">);</span>
<span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">Keywords</span><span class="special">&gt;</span> <span class="identifier">init</span><span class="special">(</span><span class="identifier">Keywords</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">kw</span><span class="special">,</span> <span class="keyword">char</span> <span class="keyword">const</span><span class="special">*</span> <span class="identifier">doc</span> <span class="special">=</span> <span class="number">0</span><span class="special">);</span>
<span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">Keywords</span><span class="special">&gt;</span> <span class="identifier">init</span><span class="special">(</span><span class="keyword">char</span> <span class="keyword">const</span><span class="special">*</span> <span class="identifier">doc</span><span class="special">,</span> <span class="identifier">Keywords</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">kw</span><span class="special">);</span>
</pre>
<div class="variablelist">
<p class="title"><b></b></p>
<dl class="variablelist">
<dt><span class="term">Requires</span></dt>
<dd><p>
                  If supplied, doc is an <a class="link" href="../glossary.html#ntbs">ntbs</a>. If supplied,
                  kw is the result of a
                </p></dd>
<dt><span class="term">Effects</span></dt>
<dd>
<p>
                  The result is an init-expression whose docstring is doc and whose
                  keywords are a reference to kw. If the first form is used, the
                  resulting expression's keywords are empty. The expression's call
                  policies are an instance of <a class="link" href="../function_invocation_and_creation/models_of_callpolicies.html#function_invocation_and_creation.models_of_callpolicies.boost_python_default_call_polici" title="boost/python/default_call_policies.hpp">default_call_policies</a>.
                  If Tn is <a class="link" href="boost_python_init_hpp.html#high_level_components.boost_python_init_hpp.class_template_optional" title="Class template optional">optional&lt;U1,
                  U2,... Um&gt;</a>, the expression's valid prefixes are given
                  by:
                </p>
<pre class="programlisting"><span class="special">(</span><span class="identifier">T1</span><span class="special">,</span> <span class="identifier">T2</span><span class="special">,...</span><span class="identifier">Tn</span><span class="special">-</span><span class="number">1</span><span class="special">),</span> <span class="special">(</span><span class="identifier">T1</span><span class="special">,</span> <span class="identifier">T2</span><span class="special">,...</span><span class="identifier">Tn</span><span class="special">-</span><span class="number">1</span> <span class="special">,</span> <span class="identifier">U1</span><span class="special">),</span> <span class="special">(</span><span class="identifier">T1</span><span class="special">,</span> <span class="identifier">T2</span><span class="special">,...</span><span class="identifier">Tn</span><span class="special">-</span><span class="number">1</span> <span class="special">,</span> <span class="identifier">U1</span><span class="special">,</span> <span class="identifier">U2</span><span class="special">),</span> <span class="special">...(</span><span class="identifier">T1</span><span class="special">,</span> <span class="identifier">T2</span><span class="special">,...</span><span class="identifier">Tn</span><span class="special">-</span><span class="number">1</span> <span class="special">,</span> <span class="identifier">U1</span><span class="special">,</span> <span class="identifier">U2</span><span class="special">,...</span><span class="identifier">Um</span><span class="special">)</span></pre>
<p>
                  . Otherwise, the expression has one valid prefix given by the template
                  arguments the user specified.
                </p>
</dd>
</dl>
</div>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="high_level_components.boost_python_init_hpp.class_template_init.class_template_init_observer_fun"></a><a class="link" href="boost_python_init_hpp.html#high_level_components.boost_python_init_hpp.class_template_init.class_template_init_observer_fun" title="Class template init observer functions">Class
          template <code class="computeroutput"><span class="identifier">init</span></code> observer
          functions</a>
</h4></div></div></div>
<pre class="programlisting"><span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">Policies</span><span class="special">&gt;</span>
<span class="identifier">unspecified</span> <span class="keyword">operator</span><span class="special">[](</span><span class="identifier">Policies</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">policies</span><span class="special">)</span> <span class="keyword">const</span>
</pre>
<div class="variablelist">
<p class="title"><b></b></p>
<dl class="variablelist">
<dt><span class="term">Requires</span></dt>
<dd><p>
                  Policies is a model of <a class="link" href="../concepts.html#concepts.callpolicies" title="CallPolicies">CallPolicies</a>.
                </p></dd>
<dt><span class="term">Effects</span></dt>
<dd><p>
                  Returns a new <a class="link" href="boost_python_init_hpp.html#high_level_components.boost_python_init_hpp.introduction.init_expressions" title="init-expressions">init-expression</a>
                  with all the same properties as the init object except that its
                  call policies are replaced by a reference to policies.
                </p></dd>
</dl>
</div>
</div>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="high_level_components.boost_python_init_hpp.class_template_optional"></a><a class="link" href="boost_python_init_hpp.html#high_level_components.boost_python_init_hpp.class_template_optional" title="Class template optional">Class
        template <code class="computeroutput"><span class="identifier">optional</span></code> </a>
</h3></div></div></div>
<p>
          A MPL sequence which can be used to specify the optional arguments to an
          __init__ function.
        </p>
<pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">python</span>
<span class="special">{</span>
  <span class="keyword">template</span> <span class="special">&lt;</span><span class="identifier">T1</span> <span class="special">=</span> <span class="identifier">unspecified</span><span class="special">,...</span><span class="identifier">Tn</span> <span class="special">=</span> <span class="identifier">unspecified</span><span class="special">&gt;</span>
  <span class="keyword">struct</span> <span class="identifier">optional</span> <span class="special">{};</span>
<span class="special">}}</span>
</pre>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="high_level_components.boost_python_init_hpp.example"></a><a class="link" href="boost_python_init_hpp.html#high_level_components.boost_python_init_hpp.example" title="Example">Example</a>
</h3></div></div></div>
<p>
          Given the C++ declarations:
        </p>
<pre class="programlisting"><span class="keyword">class</span> <span class="identifier">Y</span><span class="special">;</span>
<span class="keyword">class</span> <span class="identifier">X</span>
<span class="special">{</span>
 <span class="keyword">public</span><span class="special">:</span>
   <span class="identifier">X</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">x</span><span class="special">,</span> <span class="identifier">Y</span><span class="special">*</span> <span class="identifier">y</span><span class="special">)</span> <span class="special">:</span> <span class="identifier">m_y</span><span class="special">(</span><span class="identifier">y</span><span class="special">)</span> <span class="special">{}</span>
   <span class="identifier">X</span><span class="special">(</span><span class="keyword">double</span><span class="special">);</span>
 <span class="keyword">private</span><span class="special">:</span>
   <span class="identifier">Y</span><span class="special">*</span> <span class="identifier">m_y</span><span class="special">;</span>
<span class="special">};</span>
</pre>
<p>
          A corresponing Boost.Python extension class can be created with:
        </p>
<pre class="programlisting"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">python</span><span class="special">;</span>

<span class="identifier">class_</span><span class="special">&lt;</span><span class="identifier">X</span><span class="special">&gt;(</span><span class="string">"X"</span><span class="special">,</span> <span class="string">"This is X's docstring."</span><span class="special">,</span>
          <span class="identifier">init</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">,</span><span class="keyword">char</span> <span class="keyword">const</span><span class="special">*&gt;(</span><span class="identifier">args</span><span class="special">(</span><span class="string">"x"</span><span class="special">,</span><span class="string">"y"</span><span class="special">),</span> <span class="string">"X.__init__'s docstring"</span><span class="special">)[</span>
                <span class="identifier">with_custodian_and_ward</span><span class="special">&lt;</span><span class="number">1</span><span class="special">,</span><span class="number">3</span><span class="special">&gt;()]</span>
          <span class="special">)</span>
   <span class="special">.</span><span class="identifier">def</span><span class="special">(</span><span class="identifier">init</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">&gt;())</span>
   <span class="special">;</span>
</pre>
</div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2002-2005, 2015 David Abrahams, Stefan Seefeld<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="boost_python_exception_translato.html"><img src="../../images/prev.png" alt="Prev"></a><a accesskey="u" href="../high_level_components.html"><img src="../../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../images/home.png" alt="Home"></a><a accesskey="n" href="boost_python_iterator_hpp.html"><img src="../../images/next.png" alt="Next"></a>
</div>
</body>
</html>
